﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary

Public Class ucAccessory
    Dim db As New TanNamChinhDataContext
    Private m_ac As New CAccessory
    Private m_Type As Integer '1 : Accessory of Truck, 2: Accessory of Cont

    Public Sub New(ByVal Type As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_Type = Type
        DataGridViewHelper.DataGridView_InitEvent(dtgAccessory)
    End Sub

    Private Sub frmDMPhuTung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KhoiTao_GiaoDien(True)
        KhoiTaodtgAccessory()
    End Sub

    Public Sub KhoiTaodtgAccessory(Optional ByVal NameUpdate As String = "", Optional ByVal Name As String = "")
        Try
            Dim fc = m_ac.SelectAllRecordByTypeAndName(m_Type, Name)

            Dim source As New BindingSource
            source.DataSource = fc
            BindingNavigator1.BindingSource = source

            dtgAccessory.DataSource = source
            FormatdtgAccessory()

            If NameUpdate = "" Then
                dtgAccessory.Rows(0).Selected = True
                dtgAccessory.FirstDisplayedScrollingRowIndex = 0
            Else
                Dim index As Integer = 0
                For i As Integer = 0 To dtgAccessory.RowCount - 1
                    If dtgAccessory.Rows(i).Cells("AccessoryName").Value = NameUpdate Then
                        index = i
                        Exit For
                    End If
                Next

                dtgAccessory.Rows(index).Cells("AccessoryName").Selected = True
                'dtgAccessory.FirstDisplayedScrollingRowIndex = index

            End If
            
        Catch ex As Exception

        End Try
    End Sub

    Public Sub FormatdtgAccessory()
        'dtgAccessory.RowHeadersWidth = 20
        dtgAccessory.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgAccessory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        dtgAccessory.MultiSelect = False
        dtgAccessory.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgAccessory.ReadOnly = True
        dtgAccessory.BackgroundColor = Color.White
        dtgAccessory.AllowUserToAddRows = False
        dtgAccessory.AllowUserToDeleteRows = False

        dtgAccessory.Columns("ID_Accessory").Visible = False
        dtgAccessory.Columns("IsActive").Visible = False
        dtgAccessory.Columns("Type").Visible = False
        With dtgAccessory.Columns("AccessoryName")
            .HeaderText = "Accessory"
        End With
    End Sub

#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Clear()
        txtAccessory.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtAccessory.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_Accessory As Integer = dtgAccessory.CurrentRow.Cells("ID_Accessory").Value
        'kiem tra truoc khi xoa
        Dim kt = (From p In db.tblTruckAccessories
                 Where p.ID_Accessory = ID_Accessory
                 Select p).Count

        If kt > 0 Then
            MsgBox("Phụ tùng này đang được sử dụng, không xóa được")
            Exit Sub
        End If

        Dim FCName As String = dtgAccessory.CurrentRow.Cells("AccessoryName").Value
        If (MsgBox("Bạn muốn xóa " + FCName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_ac.Delete(ID_Accessory)
                KhoiTaodtgAccessory(0)
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Accessory Name {0}", FCName))
            Catch ex As Exception
                MsgBox("Del - Accessory: " + FCName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim obj_ac As New tblAccessory
        obj_ac.AccessoryName = txtAccessory.Text
        obj_ac.Type = m_Type

        If f_Them Then
            obj_ac.IsActive = True
            m_ac.Insert(obj_ac)
            KhoiTaodtgAccessory(txtAccessory.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Accessory Name {0}", obj_ac.AccessoryName))
        End If

        If f_Sua Then
            obj_ac.ID_Accessory = dtgAccessory.CurrentRow.Cells("ID_Accessory").Value
            m_ac.Update(obj_ac)
            KhoiTaodtgAccessory(txtAccessory.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Accessory Name {0}", obj_ac.AccessoryName))
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        KhoiTaodtgAccessory(dtgAccessory.CurrentRow.Cells("AccessoryName").Value, txtNameSearch.Text)
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        txtAccessory.ReadOnly = flag
        
        dtgAccessory.Enabled = flag

        'If flag Then
        '    txtAccessory.BackColor = Color.Azure
        'Else
        '    txtAccessory.BackColor = Color.White
        'End If
    End Sub

    Public Sub Clear()
        txtAccessory.Text = ""
    End Sub
#End Region

    Private Sub dtgAccessory_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgAccessory.CurrentCellChanged
        Try
            txtAccessory.Text = dtgAccessory.CurrentRow.Cells("AccessoryName").Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        KhoiTaodtgAccessory("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        KhoiTaodtgAccessory("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub txtNameSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            KhoiTaodtgAccessory("", txtNameSearch.Text.Trim)
        End If
    End Sub
End Class
